/* Title:		Mail send package
 *
 * Project:     MAS
 *
 * @ Link: 		http://www.smarter.com
 *
 * @ Email:		ivan_ling@mezimedia.com
 *
 * @ Copyright: Copyright (c) 2007 mezimedia
 *
 * @ Author 	Ivan.ling
 *
 * @ Version 	2.0
 */
package com.pub.mail;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
import java.util.Map.Entry;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import com.pub.util.Validate;
import com.pub.util.XMLUtils;

//import com.mezimedia.util.Validate;
//import com.mezimedia.util.XMLUtils;


public class Mail
{
	public static final String STR_CONFIG_FILES_PATH_NAME_FOR_SYSTEM_ENV = "CPSXMLCFG_ROOT";
	
	public static final String TAG_SERVER 			= "Default";
	public static final String TAG_SERVER_NAME		= "ServerName";
	
	public static final String TAG_LEVEL 			= "Level";
	public static final String TAG_FROM 			= "Send";
	public static final String TAG_TOS 				= "ReceiveTO";
	public static final String TAG_CC 				= "ReceiveCC";
	public static final String TAG_BCC 				= "ReceiveBCC";
	public static final String TAG_ATTACH 			= "Attach";
	public static final String TAG_SUBJECT 			= "Subject";
	public static final String TAG_CONTENT_TYPE		= "ContentType";
	
	public static final String TAG_SEND_RECEIVE 	= "Default";
	public static final String TEST_SEND_RECEIVE 		= "Test";	
	
	public static enum EnumPriorityLevel {LOWEST_GREEN, LOW_BLUE, COMMON_YELLOW, HIGH_ORANGE, HIGHEST_RED};
	
	protected static String ConfigFileDir;
	
	protected static Logger logger;

	protected Map<String, Map<String, String>> hmServerList;
	protected Map<String, Map<String, String>> hmSendReceiveList;
	protected Map<String, Map<String, String>> hmTempateList;
	protected Map<String, Session> hmServerSession;

	protected MimeMessage mmMailMsg;
	protected StringBuilder sbBodyCache;
	
	protected String strSubject 	= null;
	protected String strFrom 		= null;
	protected String strTOs 		= null;
	protected String strCCs 		= null;
	protected String strBCCs 		= null;
	protected String FromDisplayName= null;
	
	protected String strTemlatePath = null;
	protected EnumPriorityLevel emPriority	= null;
	
	protected ArrayList<MimeBodyPart> alAllAttachFile = new ArrayList<MimeBodyPart>();
	
	VelocityEngine 	veVelocity;
	VelocityContext vcContextSource;

	static
	{
		logger = Logger.getLogger(Mail.class.getName());
	}
	
	public static void main(String args[])
	{
		Mail mlSendMail = new Mail();
		
		mlSendMail.addContent("test");
		boolean isSend = mlSendMail.sendDefault(Mail.TAG_SERVER, "System_Service");
		if(isSend){
			logger.info("send mail success ..........................................");
		}else{
			logger.info("send mail failure ..........................................");
			
		}
	}

	public Mail() {
		init();
		sbBodyCache = new StringBuilder(1000);
	}

	public Mail(String configFilePath) 
	{
		ConfigFileDir = configFilePath;
		
		init();
		sbBodyCache = new StringBuilder(1000);
	}
	
	public boolean init() 
	{
		boolean isReturn = false;
		
		/*-- < begin > parse mail property of XML configure < begin > --*/
    	hmServerList 		= new HashMap<String, Map<String, String>>();
    	hmSendReceiveList 	= new HashMap<String, Map<String, String>>();
    	hmTempateList 		= new HashMap<String, Map<String, String>>();
    	hmServerSession 	= new HashMap<String, Session>();
    	 
		InputStream fis 	= null;
		
    	try {
    		ConfigFileDir = System.getenv(STR_CONFIG_FILES_PATH_NAME_FOR_SYSTEM_ENV);
    	    
    	    if(ConfigFileDir == null){
    	    	logger.fatal("can't find CPSXMLCFG_ROOT from system env.");
    	    	
    	    	return isReturn;
    	    }
    	    
    	    if (ConfigFileDir.indexOf(ConfigFileDir.length()-1) != '/'){
    	    	ConfigFileDir = ConfigFileDir + "/";
	    	}
    	    
    	    DOMConfigurator.configure(ConfigFileDir + "log4j.xml");
    	    
        	logger.debug("mail.xml path: " + ConfigFileDir + "mail.xml");
        	XMLUtils xmlUtils = new XMLUtils();
        	
        	/**-- fetch Server list --**/
        	Document doc = xmlUtils.parseXMLDocument(ConfigFileDir + "mail.xml");
			if(doc == null){
				System.out.println("can't found mail.xml from path " + ConfigFileDir + "mail.xml");
				
				return isReturn;
			}else{
				Vector<Node> ServerList = xmlUtils.getAllElement(doc, "Server");
			
				for(Node nd : ServerList){
					Map<String, String> serverData = xmlUtils.getNameTextContent(nd);
					if(serverData.size() > 0)
		            	hmServerList.put(serverData.get("NameKey"), serverData);
				}
			}
 			
			/**-- fetch send and receive mail address list --**/
			StringBuilder sbReceiveListTO  	= new StringBuilder(100);
	        StringBuilder sbReceiveListCC  	= new StringBuilder(100);
	        StringBuilder sbReceiveListBCC 	= new StringBuilder(100);
	        StringBuilder sbAttachFileList	= new StringBuilder(100);
	        
	        Vector<Node> data;
	        Map<String, String> hmSendReceiveData;
	        
	        Vector<Node> sendReceivelist = xmlUtils.getAllElement(doc, "SendReceive");
			
			for(Node nd : sendReceivelist){
		        sbReceiveListTO 	= sbReceiveListTO.delete(0,sbReceiveListTO.length());		//-- initial to primary recipients mail address list
		        sbReceiveListCC 	= sbReceiveListCC.delete(0,sbReceiveListCC.length());		//-- initial carbon copy recipients mail address list
		        sbReceiveListBCC 	= sbReceiveListBCC.delete(0,sbReceiveListBCC.length());		//-- initial blind carbon copy recipients mail address list
		        sbAttachFileList 	= sbAttachFileList.delete(0,sbAttachFileList.length());		//-- initial attach file name list

		        hmSendReceiveData = new HashMap<String, String>();
				data = xmlUtils.getElementNodes(nd);
				for(Node ndData : data){
					if(ndData.getNodeName().equalsIgnoreCase("ReceiveTO")){
	            		if(Validate.isSimpleEmail(ndData.getTextContent())){
		            		if(sbReceiveListTO.length() > 0)
		            			sbReceiveListTO.append(",");
		            		sbReceiveListTO.append(ndData.getTextContent());
		            	}else
		            		continue;	//-- add mail address error debug info 
	            	}else if(ndData.getNodeName().equalsIgnoreCase("ReceiveCC")){
	            		if(Validate.isSimpleEmail(ndData.getTextContent())){
		            		if(sbReceiveListCC.length() > 0)
		            			sbReceiveListCC.append(",");
		            		sbReceiveListCC.append(ndData.getTextContent());
		            	}else 
		            		continue;	//-- add mail address error debug info 
	            	}else if(ndData.getNodeName().equalsIgnoreCase("ReceiveBCC")){
	            		if(Validate.isSimpleEmail(ndData.getTextContent())){
		            		if(sbReceiveListBCC.length() > 0)
		            			sbReceiveListBCC.append(",");
		            		sbReceiveListBCC.append(ndData.getTextContent());
		            	}else
		            		continue;	//-- add mail address error debug info 
	            	}else if(ndData.getNodeName().equalsIgnoreCase("Attach")){
	            		if(sbAttachFileList.length() > 0)
	            			sbAttachFileList.append(",");
	            		sbAttachFileList.append(ndData.getTextContent());
		            }else{
	            		hmSendReceiveData.put(ndData.getNodeName(), ndData.getTextContent());
		            }
				}
				
				if(sbReceiveListTO.length() > 0)
	            	hmSendReceiveData.put("ReceiveTO", sbReceiveListTO.toString());		//-- add receiveTO mail address list
	            if(sbReceiveListCC.length() > 0)
	            	hmSendReceiveData.put("ReceiveCC", sbReceiveListCC.toString());		//-- add receiveCC mail address list
	            if(sbReceiveListBCC.length() > 0)
	            	hmSendReceiveData.put("ReceiveBCC", sbReceiveListBCC.toString());	//-- add receiveBCC mail address list
	            if(sbAttachFileList.length() > 0)
	            	hmSendReceiveData.put("Attach", sbAttachFileList.toString());		//-- add attach file name list
	            
	            if(hmSendReceiveData.size() >0){
	            	hmSendReceiveList.put(hmSendReceiveData.get("NameKey"), hmSendReceiveData);
	            	logger.debug("hmSendReceiveList toString : " + hmSendReceiveData.toString());
	            }
				
			}
			
			
			/*-- 	< end > parse mail property of XML config < end >
	 		 * 		< begin > register mail server session < begin >	--*/
			Vector<Node> ServerList = xmlUtils.getAllElement(doc, "Template");
			
			for(Node nd : ServerList){
				Map<String, String> templateData = xmlUtils.getNameTextContent(nd);
				if(templateData.size() > 0)
					hmTempateList.put(templateData.get("NameKey"), templateData);
			}
			
			/*-- 	< end > parse mail property of XML config < end >
	 		 * 		< begin > register mail server session < begin >	--*/

		}catch(Exception e) {
		    logger.fatal("It read mail.xml file fail. Excption : " + e.toString());
			e.printStackTrace();
			
			return isReturn;
		}finally{
			if(fis != null){
				try{
					fis.close();
				}catch(Exception e) {}
			}
		}

 		/*-- 	< end > parse mail property of XML config < end >
 		 * 		< begin > register mail server session < begin >	--*/
		for(Entry<String, Map<String, String>> serverInfoList : hmServerList.entrySet()){
			final Map<String, String> hmServerInfo = serverInfoList.getValue();
			if (hmServerInfo != null) {
				Properties ptServerProperty = new Properties();
				Session snMailSession;
				
				ptServerProperty.put("mail.smtp.host", hmServerInfo.get("Host"));
				ptServerProperty.put("mail.smtp.auth", hmServerInfo.get("IsAuth"));
				ptServerProperty.put("mail.smtp.port", hmServerInfo.get("Port"));
				
				if(hmServerInfo.get("IsAuth").equals("false"))
					snMailSession = Session.getInstance(ptServerProperty);
				else{
					snMailSession = Session.getInstance(ptServerProperty, 
									new Authenticator(){
										public PasswordAuthentication getPasswordAuthentication() {
											return new PasswordAuthentication(hmServerInfo.get("User"), hmServerInfo.get("Password"));
										}
									}
							 );
				}
				
				hmServerSession.put(serverInfoList.getKey(), snMailSession);
			}
		}
		/*-- 	< end > register mail server session < end > --*/

		isReturn = true;
		
		return isReturn;
	}

	public boolean initTemplates() 
	{
		boolean isReturn = true;
		
		/*-- < begin > parse mail property of XML configure < begin > --*/
    	hmServerList 		= new HashMap<String, Map<String, String>>();
    	hmSendReceiveList 	= new HashMap<String, Map<String, String>>();
    	hmTempateList 		= new HashMap<String, Map<String, String>>();
    	hmServerSession 	= new HashMap<String, Session>();
    	 
 		veVelocity = new VelocityEngine();
		vcContextSource = new VelocityContext();

		Properties pptVelocity 	= new Properties();
		InputStream fis 	= null;
		
    	try {
        	fis = new FileInputStream(ConfigFileDir + "velocity.properties");
 	    	pptVelocity.load(fis);
			
 	    	String strPath = pptVelocity.getProperty(Velocity.FILE_RESOURCE_LOADER_PATH);
			if (strPath == null){
				pptVelocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "templates");
			}
			
			String strLog = pptVelocity.getProperty(Velocity.RUNTIME_LOG);
			if (strLog == null){
				pptVelocity.setProperty(Velocity.RUNTIME_LOG, "velocity.logger");
			}
			
			veVelocity.init(pptVelocity);
		}catch(Exception e) {
		    logger.fatal("It read velocity.properties file failed. Excption : " + e.toString());
			e.printStackTrace();

			isReturn = false;
		}finally{
			if(fis != null){
				try{
					fis.close();
				}catch(Exception e) {}
			}
		}
		
		return isReturn;
	}
	
	
	/* send mail
	 *
	 * @return 	success: true ; fail: false
	 */
	public boolean send()
	{
		boolean isRetrun = false;
		
		final Map<String, String> hmServer = hmServerList.get(Mail.TAG_SERVER);
		
		if(hmServer == null || strSubject == null || strFrom == null || strTOs == null){
			logger.warn("parameter is empty.");
			return isRetrun;
	
		}
		
		final Session snMailSession = hmServerSession.get(hmServer.get("ServerName"));
		if(hmServerSession == null){
			logger.warn("server session is null.");
			return isRetrun;
		}else{
			mmMailMsg = new MimeMessage(snMailSession);
			try{
				if(this.emPriority != null)
					mmMailMsg.addHeader("X-Priority", this.getLevelPriority(this.emPriority));
				
				if(strFrom != null)
					mmMailMsg.setFrom(new InternetAddress(strFrom, FromDisplayName));
				else{
					final Map<String, String> hmSendReceiveInfo = hmSendReceiveList.get(Mail.TAG_SEND_RECEIVE);
					mmMailMsg.setFrom(new InternetAddress(hmSendReceiveInfo.get("Send")));
				}
				
				mmMailMsg.setSubject(strSubject);
				mmMailMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strTOs));
				
				if(strCCs != null)
					mmMailMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(strCCs));
				
				if(strBCCs != null)
					mmMailMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(strBCCs));
				
				MimeMultipart mmpBody = new MimeMultipart();
				MimeBodyPart  mbpMailContent = new MimeBodyPart();
				
				mbpMailContent.setContent(sbBodyCache.toString(), "text/html; charset=UTF-8");
				
				mmpBody.addBodyPart(mbpMailContent);
				  
				mmMailMsg.setContent(mmpBody);
				mmMailMsg.saveChanges();
				Transport.send(mmMailMsg);
				
				isRetrun = true;
			}catch(Exception e){
				logger.error("mail send failed. Exception : " + e.toString());
				e.printStackTrace();
				
				isRetrun = false;
			}
		}
		
		return isRetrun;
	}
	
	/* send mail
	 *
	 * @param 	sendReceiveKey	send receive key name
	 * @return 	success: true ; fail: false
	 */
	public boolean send(String sendReceiveKey)
	{
		boolean isReturn = false;
		
		if(hmSendReceiveList.get(sendReceiveKey) == null || hmSendReceiveList.get(sendReceiveKey).get(TAG_SERVER_NAME) == null)
			return isReturn;
		
		Session snMailSession = hmServerSession.get(hmSendReceiveList.get(sendReceiveKey).get(TAG_SERVER_NAME));
		final Map<String, String> hmSendReceiveInfo = hmSendReceiveList.get(sendReceiveKey);
		if(hmServerSession == null)
			return isReturn;
		else{
			mmMailMsg = new MimeMessage(snMailSession);
			try{
				if(hmSendReceiveInfo.get(TAG_LEVEL) != null && getLevelPriority(hmSendReceiveInfo.get(TAG_LEVEL)) != null)
					mmMailMsg.addHeader("X-Priority", this.getLevelPriority(getLevelPriority(hmSendReceiveInfo.get(TAG_LEVEL))));
				
				if(hmSendReceiveInfo.get(TAG_FROM) != null)
					mmMailMsg.setFrom(new InternetAddress(hmSendReceiveInfo.get(TAG_FROM)));
				
				if(this.getSubject() != null)
					mmMailMsg.setSubject(this.getSubject());
				else if(hmSendReceiveInfo.get(TAG_SUBJECT) != null)
					mmMailMsg.setSubject(hmSendReceiveInfo.get(TAG_SUBJECT));
				
				if(this.getTOs() != null)
					mmMailMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(this.getTOs()));
				else if(hmSendReceiveInfo.get(TAG_TOS) != null)
					mmMailMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(hmSendReceiveInfo.get(TAG_TOS)));
				
				if(this.getCCs() != null)
					mmMailMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(this.getCCs()));
				else if(hmSendReceiveInfo.get(TAG_CC) != null)
					mmMailMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(hmSendReceiveInfo.get(TAG_CC)));

				if(this.getBCCs() != null)
					mmMailMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(this.getBCCs()));
				else if(hmSendReceiveInfo.get(TAG_BCC) != null)
					mmMailMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(hmSendReceiveInfo.get(TAG_BCC)));
				
				MimeMultipart mmpBody = new MimeMultipart();
				MimeBodyPart  mbpMailContent = new MimeBodyPart();
				if(hmSendReceiveInfo.containsKey(TAG_CONTENT_TYPE))
					mbpMailContent.setContent(getContent(), "text/html; charset=UTF-8");
				else
					mbpMailContent.setContent(getContent(), hmSendReceiveInfo.get(TAG_CONTENT_TYPE));
				
				mmpBody.addBodyPart(mbpMailContent);
				
				if(alAllAttachFile.size() > 0){
					int iAttachFileCount = alAllAttachFile.size();
					for(int i = 0; i < iAttachFileCount; i++){
						mmpBody.addBodyPart(alAllAttachFile.get(i));
					}
				}
				
				mmMailMsg.setContent(mmpBody);
				mmMailMsg.saveChanges();
				Transport.send(mmMailMsg);

				isReturn = true;
			}catch(Exception e){
				logger.error("mail send failed. send receive key : " + sendReceiveKey + ", Exception : " + e.toString());
				e.printStackTrace();
				
				isReturn = false;
			}
		}
		
		return isReturn;
	}

	/* send mail
	 *
	 * @param 		mailServerName	mail server name
	 * @param 		sendReceiveKey	send receive key name
	 * @return 	success: true ; fail: false
	 */
	public boolean send(String mailServerName, String sendReceiveKey)
	{
		boolean isReturn = false;
		
		if(mailServerName == null || sendReceiveKey == null)
			return isReturn;
		
		Session snMailSession = hmServerSession.get(mailServerName);
		final Map<String, String> hmSendReceiveInfo = hmSendReceiveList.get(sendReceiveKey);
		if(hmServerSession == null)
			return isReturn;
		else{
			mmMailMsg = new MimeMessage(snMailSession);
			try{
				if(hmSendReceiveInfo.get(TAG_LEVEL) != null && getLevelPriority(hmSendReceiveInfo.get(TAG_LEVEL)) != null)
					mmMailMsg.addHeader("X-Priority", this.getLevelPriority(getLevelPriority(hmSendReceiveInfo.get(TAG_LEVEL))));
				
				if(hmSendReceiveInfo.get(TAG_FROM) != null)
					mmMailMsg.setFrom(new InternetAddress(hmSendReceiveInfo.get(TAG_FROM)));
					
				if(hmSendReceiveInfo.get(TAG_SUBJECT) != null)
					mmMailMsg.setSubject(hmSendReceiveInfo.get(TAG_SUBJECT));
				
				if(hmSendReceiveInfo.get(TAG_TOS) != null)
					mmMailMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(hmSendReceiveInfo.get(TAG_TOS)));
				
				if(hmSendReceiveInfo.get(TAG_CC) != null)
					mmMailMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(hmSendReceiveInfo.get(TAG_CC)));

				if(hmSendReceiveInfo.get(TAG_BCC) != null)
					mmMailMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(hmSendReceiveInfo.get(TAG_BCC)));
				
				MimeMultipart mmpBody = new MimeMultipart();
				MimeBodyPart  mbpMailContent = new MimeBodyPart();
				if(hmSendReceiveInfo.containsKey(TAG_CONTENT_TYPE))
					mbpMailContent.setContent(getContent(), "text/html; charset=UTF-8");
				else
					mbpMailContent.setContent(getContent(), hmSendReceiveInfo.get(TAG_CONTENT_TYPE));
				
				mmpBody.addBodyPart(mbpMailContent);
				
				if(alAllAttachFile.size() > 0){
					int iAttachFileCount = alAllAttachFile.size();
					for(int i = 0; i < iAttachFileCount; i++){
						mmpBody.addBodyPart(alAllAttachFile.get(i));
					}
				}
				
				mmMailMsg.setContent(mmpBody);
				mmMailMsg.saveChanges();
				Transport.send(mmMailMsg);

				isReturn = true;
			}catch(Exception e){
				logger.error("mail send failed. send receive key : " + sendReceiveKey + ", Exception : " + e.toString());
				e.printStackTrace();
				
				isReturn = false;
			}
		}
		
		return isReturn;
	}
	
	/* send mail
	 *
	 * @param 		strSubject			send subject
	 * @param 		strFrom				mail send mail
	 * @param 		strTos				receive mail list , It be spaced comma.
	 * @param 		content 			content 
	 * @return 	success: true ; fail: false
	 */
	public boolean send(String strSubject, String strFrom, String strTos, String content)
	{
		boolean isRetrun = false;
		
		final Map<String, String> hmServer = hmServerList.get(Mail.TAG_SERVER);
		
		if(hmServer == null || strSubject == null || strFrom == null || strTos == null){
			logger.warn("parameter is empty.");
			return isRetrun;
	
		}
		
		final Session snMailSession = hmServerSession.get(hmServer.get("ServerName"));
		if(hmServerSession == null){
			logger.warn("server session is null.");
			return isRetrun;
		}else{
			mmMailMsg = new MimeMessage(snMailSession);
			try{
				if(this.emPriority != null)
					mmMailMsg.addHeader("X-Priority", this.getLevelPriority(this.emPriority));
				
				if(strFrom != null)
					mmMailMsg.setFrom(new InternetAddress(strFrom));
				else{
					final Map<String, String> hmSendReceiveInfo = hmSendReceiveList.get(Mail.TAG_SEND_RECEIVE);
					mmMailMsg.setFrom(new InternetAddress(hmSendReceiveInfo.get("Send")));
				}
				
				mmMailMsg.setSubject(strSubject);
				mmMailMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strTos));
				
				MimeMultipart mmpBody = new MimeMultipart();
				MimeBodyPart  mbpMailContent = new MimeBodyPart();
				
				mbpMailContent.setContent(content, "text/html; charset=UTF-8");
				
				mmpBody.addBodyPart(mbpMailContent);
				  
				mmMailMsg.setContent(mmpBody);
				mmMailMsg.saveChanges();
				Transport.send(mmMailMsg);
				
				isRetrun = true;
			}catch(Exception e){
				logger.error("mail send failed. Exception : " + e.toString());
				e.printStackTrace();
				
				isRetrun = false;
			}
		}
		
		return isRetrun;
	}
	
	/* send mail
	 *
	 * @param 		strSubject			send subject
	 * @param 		strFrom				mail send mail
	 * @param 		strTos				receive mail list , It be spaced comma.
	 * @param 		content 			content 
	 * @return 	success: true ; fail: false
	 */
	public boolean send(String strSubject, String strFrom, String FromDisplayName, String strTos, String content)
	{
		boolean isRetrun = false;
		
		final Map<String, String> hmServer = hmServerList.get(Mail.TAG_SERVER);
		
		if(hmServer == null || strSubject == null || strFrom == null || strTos == null){
			logger.warn("parameter is empty.");
			return isRetrun;
	
		}
		
		final Session snMailSession = hmServerSession.get(hmServer.get("ServerName"));
		if(hmServerSession == null){
			logger.warn("server session is null.");
			return isRetrun;
		}else{
			mmMailMsg = new MimeMessage(snMailSession);
			try{
				if(this.emPriority != null)
					mmMailMsg.addHeader("X-Priority", this.getLevelPriority(this.emPriority));
				
				if(strFrom != null)
					mmMailMsg.setFrom(new InternetAddress(strFrom, FromDisplayName));
				else{
					final Map<String, String> hmSendReceiveInfo = hmSendReceiveList.get(Mail.TAG_SEND_RECEIVE);
					mmMailMsg.setFrom(new InternetAddress(hmSendReceiveInfo.get("Send")));
				}
				
				mmMailMsg.setSubject(strSubject);
				mmMailMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strTos));
				
				MimeMultipart mmpBody = new MimeMultipart();
				MimeBodyPart  mbpMailContent = new MimeBodyPart();
				
				mbpMailContent.setContent(content, "text/html; charset=UTF-8");
				
				mmpBody.addBodyPart(mbpMailContent);
				  
				mmMailMsg.setContent(mmpBody);
				mmMailMsg.saveChanges();
				Transport.send(mmMailMsg);
				
				isRetrun = true;
			}catch(Exception e){
				logger.error("mail send failed. Exception : " + e.toString());
				e.printStackTrace();
				
				isRetrun = false;
			}
		}
		
		return isRetrun;
	}
	
	/* send mail
	 *
	 * @param 		strSubject			send subject
	 * @param 		strFrom				mail send mail
	 * @param 		strTos				receive mail list , It be spaced comma.
	 * @param 		strCcs				receive mail list , It be spaced comma.
	 * @param 		strBccs				receive mail list , It be spaced comma.
	 * @param 		content 			content 
	 * @return 	success: true ; fail: false
	 */
	public boolean send(String strSubject, String strFrom, String FromDisplayName, String strTos, String strCcs, String strBccs, String content)
	{
		boolean isRetrun = false;
		
		final Map<String, String> hmServer = hmServerList.get(Mail.TAG_SERVER);
		
		if(hmServer == null || strSubject == null || strFrom == null || strTos == null){
			logger.warn("parameter is empty.");
			return isRetrun;
	
		}
		
		final Session snMailSession = hmServerSession.get(hmServer.get("ServerName"));
		if(hmServerSession == null){
			logger.warn("server session is null.");
			return isRetrun;
		}else{
			mmMailMsg = new MimeMessage(snMailSession);
			try{
				if(this.emPriority != null)
					mmMailMsg.addHeader("X-Priority", this.getLevelPriority(this.emPriority));
				
				if(strFrom != null)
					mmMailMsg.setFrom(new InternetAddress(strFrom, FromDisplayName));
				else{
					final Map<String, String> hmSendReceiveInfo = hmSendReceiveList.get(Mail.TAG_SEND_RECEIVE);
					mmMailMsg.setFrom(new InternetAddress(hmSendReceiveInfo.get("Send")));
				}
				
				mmMailMsg.setSubject(strSubject);
				mmMailMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strTos));
				
				if(strCcs != null)
					mmMailMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(strCcs));
				if(strBccs != null)
					mmMailMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(strBccs));
				
				MimeMultipart mmpBody = new MimeMultipart();
				MimeBodyPart  mbpMailContent = new MimeBodyPart();
				
				mbpMailContent.setContent(content, "text/html; charset=UTF-8");
				
				mmpBody.addBodyPart(mbpMailContent);
				  
				mmMailMsg.setContent(mmpBody);
				mmMailMsg.saveChanges();
				Transport.send(mmMailMsg);
				
				isRetrun = true;
			}catch(Exception e){
				logger.error("mail send failed. Exception : " + e.toString());
				e.printStackTrace();
				
				isRetrun = false;
			}
		}
		
		return isRetrun;
	}
	
	/* send mail	use XML configure file set
	 *
	 * @param 		strServer		mail server name
	 * @param 		strSendReceive	send receive key name
	 * @return 	success: true ; fail: false
	 */
	public boolean send(String strServer, String strSendReceive, HashMap<String, String> hmVarContent)
	{
		boolean isRetrun = false;
		
		final Map<String, String> hmServer 			= hmServerList.get(strServer);
		final Map<String, String> hmSendReceiveInfo = hmSendReceiveList.get(strSendReceive);
		
		if(hmServer == null || hmSendReceiveInfo == null || hmVarContent == null)
			return isRetrun;
		
		final Session snMailSession = hmServerSession.get(hmServer.get("ServerName"));
		if(hmServerSession == null)
			return isRetrun;
		else{
			mmMailMsg = new MimeMessage(snMailSession);
			try{
				if(strFrom != null)
					mmMailMsg.setFrom(new InternetAddress(strFrom));
				else
					mmMailMsg.setFrom(new InternetAddress(hmSendReceiveInfo.get("Send")));
					
				if(strSubject != null)
					mmMailMsg.setSubject(strSubject);
				else
					if(hmSendReceiveInfo.containsKey("Subject"))
						mmMailMsg.setSubject(hmSendReceiveInfo.get("Subject"));
				
				if(strTOs != null)
					mmMailMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strTOs));
				else
					mmMailMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(hmSendReceiveInfo.get("ReceiveTO")));
				
				if(strCCs != null)
					mmMailMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(strCCs));
				else
					if(hmSendReceiveInfo.containsKey("ReceiveCC"))
						mmMailMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(hmSendReceiveInfo.get("ReceiveCC")));
				
				if(strBCCs != null)
					mmMailMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strBCCs));
				else
					if(hmSendReceiveInfo.containsKey("ReceiveBCC"))
						mmMailMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(hmSendReceiveInfo.get("ReceiveBCC")));
				
				/**-- template process --**/
				StringWriter swTemplateContent = null;
				
				if(hmVarContent.size() > 0 && hmSendReceiveInfo.containsKey("TemplateKey") && hmTempateList.containsKey(hmSendReceiveInfo.get("TemplateKey"))){
					VelocityContext context = (VelocityContext)vcContextSource.clone();
					
					for(Entry<String, String> entry : hmVarContent.entrySet()){
						context.put(entry.getKey(), entry.getValue());
					}
					
					swTemplateContent = new StringWriter();
					Map<String, String> hmTemlateInfo = hmTempateList.get(hmSendReceiveInfo.get("TemplateKey"));
					
					if(veVelocity.resourceExists(hmTemlateInfo.get("FileName"))){
						Template template = veVelocity.getTemplate(hmTemlateInfo.get("FileName")); 
						template.merge(context, swTemplateContent);
					}else
						logger.warn("template is't existed. file name is " + hmTemlateInfo.get("FileName"));
				}
				
				MimeMultipart mmpBody = new MimeMultipart();
				MimeBodyPart  mbpMailContent = new MimeBodyPart();
				
				if(hmSendReceiveInfo.containsKey("ContentType")){
					if(swTemplateContent == null)
						mbpMailContent.setContent(sbBodyCache.toString(), "text/html; charset=ISO8859_1");
					else
						mbpMailContent.setContent(swTemplateContent.toString(), "text/html; charset=UTF-8");
				}else{
					if(swTemplateContent == null)
						mbpMailContent.setContent(sbBodyCache.toString(), hmSendReceiveInfo.get("ContentType"));
					else
						mbpMailContent.setContent(swTemplateContent.toString(), hmSendReceiveInfo.get("ContentType"));
				}
				
				mmpBody.addBodyPart(mbpMailContent);
				
				if(hmSendReceiveInfo.containsKey("Attach")){
					String[] arrFilesName = hmSendReceiveInfo.get("Attach").split(",");
					
					for(int i = 0; i < arrFilesName.length; i++){
						if(new File(arrFilesName[i]).exists()){	//-- test attach is exist ?
							MimeBodyPart  mbpMailAttach = new MimeBodyPart();
							try{
								mbpMailAttach.attachFile(arrFilesName[i]);
								mmpBody.addBodyPart(mbpMailAttach);
							}catch(IOException ioe){
								logger.warn("add Attach file fail: " + ioe.toString());
							}catch(MessagingException me){
								logger.warn("add Attach file fail: " + me.toString());
							}catch(Exception e){
								logger.warn("add Attach file fail: " + e.toString());
							}
						}
					}
				}
				
				mmMailMsg.setContent(mmpBody);
				mmMailMsg.saveChanges();
				Transport.send(mmMailMsg);

				isRetrun = true;
			}catch(Exception e){
				logger.error("mail send failed. send receive key : " + strSendReceive + ", Exception : " + e.toString());
				e.printStackTrace();
				
				isRetrun = false;
			}
		}
		
		return isRetrun;
	}

	/* send mail
	 *
	 * @param 		strSubject			send subject
	 * @param 		strFrom				mail send mail
	 * @param 		strTos				receive mail list , It be spaced comma.
	 * @param 		strCcs				receive mail list , It be spaced comma.
	 * @param 		strBccs				receive mail list , It be spaced comma.
	 * @param 		hmVarContent 		content 
	 * @param 		strTemplateName 	velocity template 
	 * @return 	success: true ; fail: false
	 */
	public boolean send(String strSubject, String strFrom, String strTos, String strCcs, String strBccs, HashMap<String, Object> hmVarContent, String templateKey)
	{
		boolean isRetrun = false;;
		
		final Map<String, String> hmServer = hmServerList.get(Mail.TAG_SERVER);
		
		if(hmServer == null || strSubject == null || strFrom == null || strTos == null){
			logger.warn("parameter is empty.");
			return isRetrun;
	
		}
		
		final Session snMailSession = hmServerSession.get(hmServer.get("ServerName"));
		if(hmServerSession == null){
			logger.warn("server session is null.");
			return isRetrun;
		}else{
			mmMailMsg = new MimeMessage(snMailSession);
			try{
				if(this.emPriority != null)
					mmMailMsg.addHeader("X-Priority", this.getLevelPriority(this.emPriority));
				
				if(strFrom != null)
					mmMailMsg.setFrom(new InternetAddress(strFrom));
				else{
					final Map<String, String> hmSendReceiveInfo = hmSendReceiveList.get(Mail.TAG_SEND_RECEIVE);
					mmMailMsg.setFrom(new InternetAddress(hmSendReceiveInfo.get("Send")));
				}
				
				mmMailMsg.setSubject(strSubject);
				mmMailMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strTos));
				
				if(strCcs != null)
					mmMailMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(strCcs));
				if(strBccs != null)
					mmMailMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(strBccs));
				
				/**-- template process --**/
				StringWriter swTemplateContent = null;
				
				if(hmTempateList.containsKey(templateKey)){
					VelocityContext context = (VelocityContext)vcContextSource.clone();
					
					if(hmVarContent != null){
						for(Entry<String, Object> entry : hmVarContent.entrySet()){
							context.put(entry.getKey(), entry.getValue());
						}
					}
					
					swTemplateContent = new StringWriter();
					Map<String, String> hmTemlateInfo = hmTempateList.get(templateKey);
					
					if(veVelocity.resourceExists(hmTemlateInfo.get("FileName"))){
						Template template = veVelocity.getTemplate(hmTemlateInfo.get("FileName")); 
						template.merge(context, swTemplateContent);
						
						MimeMultipart mmpBody = new MimeMultipart();
						MimeBodyPart  mbpMailContent = new MimeBodyPart();
						
						mbpMailContent.setContent(swTemplateContent.toString(), "text/html; charset=UTF-8");
						
						mmpBody.addBodyPart(mbpMailContent);
						  
						mmMailMsg.setContent(mmpBody);
						mmMailMsg.saveChanges();
						Transport.send(mmMailMsg);

						isRetrun = true;
					}else
						logger.warn("template isn't existed. file name is " + hmTemlateInfo.get("FileName"));
				}else{
					logger.warn("template key isn't find for " + templateKey);
					logger.warn("template key list " + hmTempateList.toString());
				}
			}catch(Exception e){
				logger.error("mail send failed. Exception : " + e.toString());
				e.printStackTrace();
				
				return false;
			}
		}
		
		return isRetrun;
	}
	
	/* send mail
	 *
	 * @param 		strSubject			send subject
	 * @param 		strFrom				mail send mail
	 * @param 		strTos				receive mail list , It be spaced comma.
	 * @param 		strCcs				receive mail list , It be spaced comma.
	 * @param 		strBccs				receive mail list , It be spaced comma.
	 * @param 		hmVarContent 		content 
	 * @param 		strTemplateName 	velocity template 
	 * @return 	success: true ; fail: false
	 */
	public boolean send(String strSubject, String strFrom, String FromDisplayName, String strTos, String strCcs, String strBccs, HashMap<String, Object> hmVarContent, String templateKey)
	{
		boolean isRetrun = false;
		
		final Map<String, String> hmServer = hmServerList.get(Mail.TAG_SERVER);
		
		if(hmServer == null || strSubject == null || strFrom == null || strTos == null){
			logger.warn("parameter is empty.");
			return isRetrun;
	
		}
		
		final Session snMailSession = hmServerSession.get(hmServer.get("ServerName"));
		if(hmServerSession == null){
			logger.warn("server session is null.");
			return isRetrun;
		}else{
			mmMailMsg = new MimeMessage(snMailSession);
			try{
				if(this.emPriority != null)
					mmMailMsg.addHeader("X-Priority", this.getLevelPriority(this.emPriority));
				
				if(strFrom != null)
					mmMailMsg.setFrom(new InternetAddress(strFrom, FromDisplayName));
				else{
					final Map<String, String> hmSendReceiveInfo = hmSendReceiveList.get(Mail.TAG_SEND_RECEIVE);
					mmMailMsg.setFrom(new InternetAddress(hmSendReceiveInfo.get("Send")));
				}
				
				mmMailMsg.setSubject(strSubject);
				mmMailMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strTos));
				
				if(strCcs != null)
					mmMailMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(strCcs));
				if(strBccs != null)
					mmMailMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(strBccs));
				
				/**-- template process --**/
				StringWriter swTemplateContent = null;
				
				if(hmTempateList.containsKey(templateKey)){
					VelocityContext context = (VelocityContext)vcContextSource.clone();
					
					if(hmVarContent != null){
						for(Entry<String, Object> entry : hmVarContent.entrySet()){
							context.put(entry.getKey(), entry.getValue());
						}
					}
					
					swTemplateContent = new StringWriter();
					Map<String, String> hmTemlateInfo = hmTempateList.get(templateKey);
					
					if(veVelocity.resourceExists(hmTemlateInfo.get("FileName"))){
						Template template = veVelocity.getTemplate(hmTemlateInfo.get("FileName")); 
						template.merge(context, swTemplateContent);
						
						MimeMultipart mmpBody = new MimeMultipart();
						MimeBodyPart  mbpMailContent = new MimeBodyPart();
						
						mbpMailContent.setContent(swTemplateContent.toString(), "text/html; charset=UTF-8");
						
						mmpBody.addBodyPart(mbpMailContent);
						  
						mmMailMsg.setContent(mmpBody);
						mmMailMsg.saveChanges();
						Transport.send(mmMailMsg);

						isRetrun = true;
					}else
						logger.warn("template isn't existed. file name is " + hmTemlateInfo.get("FileName"));
				}else{
					logger.warn("template key isn't find for " + templateKey);
					logger.warn("template key list " + hmTempateList.toString());
				}
			}catch(Exception e){
				logger.error("mail send failed. Exception : " + e.toString());
				e.printStackTrace();
				
				isRetrun = false;
			}
		}
		
		return isRetrun;
	}
	
	/* send mail	use XML configure file set
	 *
	 * @param 		strServer		mail server name
	 * @param 		strSendReceive	send receive key name
	 * @ return 	success: true ; fail: false
	 */
	public boolean sendDefault(String strServer, String strSendReceive)
	{
		boolean isRetrun = false;
		
		final Map<String, String> hmServer = hmServerList.get(strServer);
		final Map<String, String> hmSendReceiveInfo = hmSendReceiveList.get(strSendReceive);
		
		if(hmServer == null || hmSendReceiveInfo == null){
			logger.warn("mail server or sender or receive is null");
			logger.warn("mail server : " + hmServer);
			logger.warn("sender or receive : " + hmSendReceiveInfo);
			
			return isRetrun;
		}
		
		final Session snMailSession = hmServerSession.get(hmServer.get("ServerName"));
		if(hmServerSession == null){
			logger.warn("mail session is null");
			return isRetrun;
		}else{
			mmMailMsg = new MimeMessage(snMailSession);
			try{
				if(this.emPriority != null)
					mmMailMsg.addHeader("X-Priority", this.getLevelPriority(this.emPriority));
				
				if(strFrom != null)
					mmMailMsg.setFrom(new InternetAddress(strFrom));
				else
					mmMailMsg.setFrom(new InternetAddress(hmSendReceiveInfo.get("Send")));
				
				if(strSubject != null)
					mmMailMsg.setSubject(strSubject);
				else
					if(hmSendReceiveInfo.containsKey("Subject"))
						mmMailMsg.setSubject(hmSendReceiveInfo.get("Subject"));
				if(strTOs != null)
					mmMailMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strTOs));
				else
					mmMailMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(hmSendReceiveInfo.get("ReceiveTO")));
				
				if(strCCs != null)
					mmMailMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(strCCs));
				else
					if(hmSendReceiveInfo.containsKey("ReceiveCC"))
						mmMailMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(hmSendReceiveInfo.get("ReceiveCC")));
				
				if(strBCCs != null)
					mmMailMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(strBCCs));
				else
					if(hmSendReceiveInfo.containsKey("ReceiveBCC"))
						mmMailMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(hmSendReceiveInfo.get("ReceiveBCC")));
				
				MimeMultipart mmpBody = new MimeMultipart();
				MimeBodyPart  mbpMailContent = new MimeBodyPart();
				
				if(hmSendReceiveInfo.containsKey("ContentType"))
					mbpMailContent.setContent(sbBodyCache.toString(), "text/html; charset=UTF-8");
				else
					mbpMailContent.setContent(sbBodyCache.toString(), hmSendReceiveInfo.get("ContentType"));
				mmpBody.addBodyPart(mbpMailContent);
				
				if(hmSendReceiveInfo.containsKey("Attach")){
					String[] arrFilesName = hmSendReceiveInfo.get("Attach").split(",");
					
					for(int i = 0; i < arrFilesName.length; i++){
						if(new File(arrFilesName[i]).exists()){	//-- test attach is exist ?
							MimeBodyPart  mbpMailAttach = new MimeBodyPart();
							try{
								mbpMailAttach.attachFile(arrFilesName[i]);
								mmpBody.addBodyPart(mbpMailAttach);
							}catch(IOException ioe){
								logger.warn("add Attach file fail: " + ioe.toString());
							}catch(MessagingException me){
								logger.warn("add Attach file fail: " + me.toString());
							}catch(Exception e){
								logger.warn("add Attach file fail: " + e.toString());
							}
						}
					}
				}
				
				if(this.alAllAttachFile.size() > 0){
					int iAttachFileCount = this.alAllAttachFile.size();
					for(int i = 0; i < iAttachFileCount; i++){
						mmpBody.addBodyPart(this.alAllAttachFile.get(i));
					}
				}
			
				mmMailMsg.setContent(mmpBody);
				mmMailMsg.saveChanges();
				Transport.send(mmMailMsg);

				isRetrun = true;
			}catch(Exception e){
				logger.error("mail send failed. Exception : " + e.toString());
				e.printStackTrace();
				
				isRetrun = false;
			}
		}
		
		return isRetrun;
	}
	
	public void clear()
	{
		this.sbBodyCache.delete(0, sbBodyCache.length());
		
		this.emPriority		= null;
		 
		this.strSubject 	= null;
		this.strFrom 		= null;
		this.strTOs 		= null;
		this.strCCs 		= null;
		this.strBCCs 		= null;
		
		this.strTemlatePath = null;
	}
	
	/* set mail subject
	 *
	 * @param 		strSubject
	 * @ return 	void
	 */
	public void setSubject(String strCustomSubject)
	{
		this.strSubject = strCustomSubject;
	}

	/* set mail subject
	 *
	 * @param 		strSubject
	 * @ return 	void
	 */
	public String getSubject()
	{
		return this.strSubject;
	}
	
	/* set from send mail address
	 *
	 * @param 		strCustomFrom
	 * @ return 	void
	 */
	public void setFrom(String strCustomFrom)
	{
		this.strFrom = strCustomFrom;
	}

	public void setDisplayName(String FromDisplayName)
	{
		this.FromDisplayName = FromDisplayName;
	}
	
	/* set mail From Display Name
	 *
	 * @param 		From Display Name
	 * @ return 	String
	 */
	public String getDisplayName()
	{
		return this.FromDisplayName;
	}
	
	/* set receive address
	 *
	 * @param 		strTOs 	to mail address list, the given comma separated sequence of $strCustomTOs
	 * @ return 	void
	 */
	public void setTOs(String strCustomTOs)
	{
		this.strTOs = strCustomTOs;
	}

	/* set receive address
	 *
	 * @param 		strTOs 	to mail address list, the given comma separated sequence of $strCustomTOs
	 * @ return 	void
	 */
	public String getTOs()
	{
		return this.strTOs;
	}
	
	/* set receive address of carbon copy 
	 *
	 * @param 		strCustomCCs 	to mail address list, the given comma separated sequence of $strCustomCCs
	 * @ return 	void
	 */
	public void setCCs(String strCustomCCs)
	{
		this.strCCs = strCustomCCs;
	}

	/* get CC list
	 *
	 * @param 		strCustomCCs 	to mail address list, the given comma separated sequence of $strCustomCCs
	 * @ return 	void
	 */
	public String getCCs()
	{
		return this.strCCs;
	}
	
	/* set receive address of binary carbon copy 
	 *
	 * @param 		strCustomBCCs 	to mail address list, the given comma separated sequence of $strCustomBCCs
	 * @ return 	void
	 */
	public void setBCCs(String strCustomBCCs)
	{
		this.strBCCs = strCustomBCCs;
	}

	/* get receive address of binary carbon copy 
	 *
	 * @param 		strCustomBCCs 	to mail address list, the given comma separated sequence of $strCustomBCCs
	 * @ return 	void
	 */
	public String getBCCs()
	{
		return this.strBCCs;
	}
	
	/* add mail body content to cache
	 *
	 * @param 		strBody 
	 * @ return 	void
	 */
	public void addContent(String strBody)
	{
		this.sbBodyCache.append(strBody);	
	}
	
	/* get mail content
	 * 
	 * @ return 	mail content
	 */
	public String getContent()
	{
		return this.sbBodyCache.toString();
	}

	/* add mail attachment
	 *
	 * @param 		strFile 	attachment file name 
	 * @ return 	void
	 */
	public void addCustomAttach(String strFile) throws IOException, MessagingException
	{
		MimeBodyPart mbAttachFile = new MimeBodyPart();
		
		mbAttachFile.attachFile(strFile);
	}
	
	
	/* set mail subject
	 *
	 * @param 		strSubject
	 * @ return 	void
	 */
	public void setPriority(EnumPriorityLevel emPriority)
	{
		this.emPriority = emPriority;
	}
	
	/* get level priority
	 *
	 * @ parameter	level 
	 * @ return 	void
	 */
	public EnumPriorityLevel getLevelPriority(String level)
	{
		if(level == null)
			return null;
		
		if(level.equalsIgnoreCase("debug"))
			return EnumPriorityLevel.LOWEST_GREEN;
		
		if(level.equalsIgnoreCase("info"))
			return EnumPriorityLevel.LOW_BLUE;
		
		if(level.equalsIgnoreCase("warn"))
			return EnumPriorityLevel.COMMON_YELLOW;
		
		if(level.equalsIgnoreCase("error"))
			return EnumPriorityLevel.HIGH_ORANGE;
		
		if(level.equalsIgnoreCase("fatal"))
			return EnumPriorityLevel.HIGHEST_RED;
		
		return null;
	}
		
	/* get level priority
	 *
	 * @param 		level 
	 * @ return 	void
	 */
	public String getLevelPriority(EnumPriorityLevel emLevel)
	{
		switch(emLevel){
			case LOWEST_GREEN : 	return "5";
			case LOW_BLUE : 		return "4";
			case COMMON_YELLOW : 	return "3";
			case HIGH_ORANGE : 		return "2";
			case HIGHEST_RED : 		return "1";
			
			default: return "";
		}
	}
	
	/* get level priority
	 *
	 * @param 		level 
	 * @ return 	void
	 */
	public EnumPriorityLevel getPriority()
	{
		return this.emPriority;
	}
	
	/* get level priority
	 *
	 * @param 		level 
	 * @ return 	void
	 */
	public String getDefaultSubject(String strSendReceive)
	{
		final Map<String, String> hmSendReceiveInfo = hmSendReceiveList.get(strSendReceive);
		if(hmSendReceiveInfo == null)
			return null;
		
		return hmSendReceiveInfo.get("Subject");
	}
	
	
	public static EnumLevel getLevelToMainPriority(EnumPriorityLevel epmLevel)
	{
		switch(epmLevel){
			case LOWEST_GREEN : 	return EnumLevel.DEBUG_GREEN;
			case LOW_BLUE : 		return EnumLevel.INFO_BLUE;
			case COMMON_YELLOW : 	return EnumLevel.WARN_YELLOW;
			case HIGH_ORANGE : 		return EnumLevel.ERROR_ORANGE;
			case HIGHEST_RED : 		return EnumLevel.FATAL_RED;
			
			default: return EnumLevel.DEBUG_GREEN;
		}
	}
}


